package com.thinkingstar.iads.projectcenter.entity.dao;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcTemplateVersion;
import com.thinkingstar.iads.projectcenter.entity.PcProjectInfo;
import com.thinkingstar.iads.projectcenter.entity.PcSjpsFile;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;


/**
 * @ClassName: DaoPcProjectSjpsData
 * @Description:设计评审
 * @author z
 */
@IocBean(args = {"refer:dao"})
public class DaoPcProjectSjpsData extends EntityRepositoryServiceImpl<DcTemplateVersion>{

    public DaoPcProjectSjpsData(Dao dao) {
        super(dao);
    }

    public List getDcTemplateVersionList(String design_content_id,String design_phase_id) {
        List list = null;
        StringBuilder find_sql = new StringBuilder("select dctv.* "
                + " from dc_template_version dctv "
                + " where dctv.del_flag = '0'"
                + " and dctv.design_content_id=@design_content_id"
                + " and dctv.design_phase_id=@design_phase_id ORDER BY template_name asc ");
        Sql sql = Sqls.create(find_sql.toString());
        sql.params().set("design_content_id", design_content_id);
        sql.params().set("design_phase_id", design_phase_id);
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List list = new ArrayList<>();
                while (rs.next()){
                    Map nutMap = new HashMap();
                    ResultSetMetaData metaData = rs.getMetaData();
                    int count = metaData.getColumnCount();
                    for (int i = count; i > 0; i--) {
                        String filed = metaData.getColumnName(i);
                        nutMap.put(filed, rs.getString(filed));
                    }
                    list.add(nutMap);
                }
                return list;
            }
        });
        dao().execute(sql);
        list = sql.getList(Map.class);
        return list;
    }


    //查询数据中心定义好的 设计评审列表
    public QueryResult
    findDesignTemplate(NutMap param){
        List <NutMap> list = null;
        QueryResult result = null;
        Pager pager = null;
        if(param == null){
            return null;
        }
        StringBuilder find_sql = new StringBuilder("select dctv.* "
                + " from dc_template_version dctv "
                + " where dctv.del_flag = '0'"
                + " and dctv.design_content_id=@design_content_id"
                + " and dctv.design_phase_id=@design_phase_id");
        Sql sql = Sqls.create(find_sql.toString());
        if(param.get("content_id") != null){
            sql.params().set("design_content_id", param.get("content_id"));
        }
        if(param.get("phase_id") != null){
            sql.params().set("design_phase_id", param.get("phase_id"));
        }
        if(param.get("page_num") != null && param.get("page_size") != null){
            pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
            pager.setRecordCount(findDesignTemplateNum(param));
            sql.setPager(pager);
        }
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<NutMap> list = new ArrayList<>();
                while (rs.next()){
                    NutMap nutMap = new NutMap();
                    ResultSetMetaData metaData = rs.getMetaData();
                    int count = metaData.getColumnCount();
                    for (int i = count; i > 0; i--) {
                        String filed = metaData.getColumnName(i);
                        nutMap.put(filed, rs.getString(filed));
                    }
                    list.add(nutMap);
                }
                return list;
            }
        });
        dao().execute(sql);
        list = sql.getList(NutMap.class);
        return new QueryResult(list,pager);
    }

    //查询数据中心定义好的 设计评审数量
    public int findDesignTemplateNum(NutMap param){
        StringBuilder find_sql = new StringBuilder("select count(dctv.id) num "
                + " from dc_template_version dctv "
                + " where dctv.del_flag = '0'"
                + " and dctv.design_content_id=@design_content_id"
                + " and dctv.design_phase_id=@design_phase_id");
        Sql sql = Sqls.create(find_sql.toString());
        if(param.get("content_id") != null){
            sql.params().set("design_content_id", param.get("content_id"));
        }
        if(param.get("phase_id") != null){
            sql.params().set("design_phase_id", param.get("phase_id"));
        }
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                int num = 0;
                while (rs.next()) {
                    num = rs.getInt("num");
                }
                return num;
            }
        });
        dao().execute(sql);
        return sql.getInt();
    }

}
